package com.djzhou.gmms.service.department.impl;

import java.util.HashMap;
import java.util.List;

import com.djzhou.gmms.model.department.TbDepartment;
import com.djzhou.gmms.model.entity.PageBean;
import com.djzhou.gmms.model.user.VoResource;
import com.djzhou.gmms.service.base.impl.BaseServiceImpl;
import com.djzhou.gmms.service.department.DepartmentService;
import com.djzhou.gmms.util.session.UserSession;

public class DepartmentServiceImpl  extends BaseServiceImpl implements DepartmentService{

	@Override
	public void add(TbDepartment t) {
		departmentDAO.insert(t);
		
	}

	@Override
	public void delete(Integer id) {
		TbDepartment department=departmentDAO.queryByID(TbDepartment.class, id);
		department.setDeleteFlag(1);
		departmentDAO.update(department);
	}

	@Override
	public TbDepartment queryById(Integer id) {
		return departmentDAO.queryByID(TbDepartment.class, id);
	}

	@Override
	public List<TbDepartment> queryList(HashMap<String, Object> hashMap) {
		String hql="from TbDepartment department where department.deleteFlag=0 order by createTime desc";
		return departmentDAO.query(hql);
	}

	@Override
	public PageBean<TbDepartment> queryPageBean(
			HashMap<String, Object> hashMap, int pageSize, int currentPage) {
		String departmentName=null;
		String companyId=null;
		String hql="from TbDepartment department where department.deleteFlag=0 and department.departmentId<>1";
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		if(hashMap!=null && hashMap.size()!=0){
			if(hashMap.get("departmentName")!=null){
				departmentName=hashMap.get("departmentName").toString();
				sb.append(" and department.departmentName like '%").append(departmentName).append("%'");
			}
			if(hashMap.get("companyId")!=null && !hashMap.get("companyId").equals(1)){
				companyId=hashMap.get("companyId").toString();
				sb.append(" and department.company.companyId=").append(companyId);
			}
		}
		
		sb.append(" order by createTime desc");
		hql=sb.toString();
		PageBean<TbDepartment> pageBean=departmentDAO.selectByPage(hql, pageSize, currentPage);
		return pageBean;
	}

	@Override
	public void update(TbDepartment t) {
		departmentDAO.update(t);
	}

	@Override
	public List<TbDepartment> queryList(Integer companyId, Integer departmentId) {
		String hql="";
		if(companyId!=1){
			hql="from TbDepartment department where department.deleteFlag=0 and department.company.companyId=? and department.departmentId<>? and department.departmentId<>1 order by createTime desc";
			return departmentDAO.query(hql,companyId,departmentId);
		}
		else {
			hql="from TbDepartment department where department.deleteFlag=0 and department.departmentId<>? order by createTime desc";
			return departmentDAO.query(hql,departmentId);
		}
	}
	@Override
	public VoResource query4AutoComplete(HashMap<String, Object> hashMap, String sidx,
			String sord, int pageSize, int currentPage) {
		VoResource resource=new VoResource();
		String searchStr="";
		if(hashMap!=null && hashMap.size()!=0){
			if(hashMap.get("departmentName")!=null){
				searchStr=hashMap.get("departmentName").toString();
			}
		}
		String hql="from TbDepartment dep where dep.deleteFlag=0 and dep.company.companyId="+UserSession.getSessionModel().getUser().getCompany().getCompanyId()+" and dep.departmentName like '%"+searchStr+"%'";
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		hql=sb.toString();
		PageBean<TbDepartment> pageBean= departmentDAO.selectByPage(hql, pageSize, currentPage);
		resource.setPage(currentPage);
		resource.setRecords(pageBean.getAllRow());
		resource.setRows(pageBean.getList());
		resource.setTotal(pageBean.getTotalPage());
		return resource;
	}

	@Override
	public List<TbDepartment> queryChildList(Integer companyId,
			Integer parentDepartmentId) {
		String hql="from TbDepartment department where department.parentDepartmentId=? and department.company.companyId=? and department.deleteFlag=0";
		return departmentDAO.query(hql, parentDepartmentId,companyId);
	}

	@Override
	public List<TbDepartment> queryAllChildList(Integer companyId,
			Integer parentDepartmentId) {
		String hql="from TbDepartment department where department.departmentPath like'%,"+parentDepartmentId+",%' and department.company.companyId=?";
		return departmentDAO.query(hql,companyId);
	}

	@Override
	public void updateDepartmentDefault(Integer departmentId) {
		String hql="update TbDepartment set defaultFlag=0 where departmentId<>?";
		departmentDAO.update(hql, departmentId);
		TbDepartment department=departmentDAO.queryByID(TbDepartment.class, departmentId);
		department.setDefaultFlag(1);
		departmentDAO.update(department);
	}

	@Override
	public List<TbDepartment> queryDepartmentByName(String departmentName) {
		// TODO Auto-generated method stub
		String hql="from TbDepartment department where department.departmentName = '"+departmentName+"' and deleteFlag=0";
		return departmentDAO.query(hql);
	}

}
